package com.panshi.util;

import com.panshi.entity.AppUserT;
import com.panshi.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserInfoUtils {

    @Autowired
    private UserClientService userClientService;

    public AppUserT getuser(HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        String userBank="";
        String userPassword="";
        if(cookies==null){
            return null;
        }
        for (Cookie cookie:cookies) {
            if(cookie.getName().equals("userBank")){
                userBank = cookie.getValue();
            }
            else if(cookie.getName().equals("userPassword")){
                userPassword = cookie.getValue();
            }
        }
        if(userBank.equals("")){
            return null;
        }

        AppUserT uservo = userClientService.getVeruser(userBank,userPassword);
        if(uservo==null){
            //没有任何信息删除用户保存信息
            for(Cookie cookie: cookies){
                cookie.setMaxAge(0);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
        }

        System.out.println("getuserinfo用戶信息"+userBank);
        System.out.println(uservo);

        return uservo;
    }


    public void veruser(HttpServletRequest request, Model model, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        String userBank="";
        String userPassword = "";
        //String userType="";   //判断是验证码还是手机号
        if(cookies==null){
            return;
        }
        for (Cookie cookie:cookies) {
            System.out.println(cookie.getValue());
            if(cookie.getName().equals("userBank")){
                userBank = cookie.getValue();
            }
            else if(cookie.getName().equals("userPassword")){
                userPassword = cookie.getValue();
            }
        }
        if(userBank==null || userBank.equals("")){
            return;
        }
        if(userPassword==null || userPassword.equals("")){
            return;
        }

        AppUserT uservo = userClientService.getVeruser(userBank,userPassword);
        System.out.println("user------------------>"+uservo);
        if(uservo!=null){
            model.addAttribute("uservo",uservo);
        }else{
            //没有任何信息删除用户保存信息
            for(Cookie cookie: cookies){
                cookie.setMaxAge(0);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
        }
        System.out.println("userinfo用戶信息"+userBank);
        System.out.println(model.getAttribute("uservo"));
        return;
    }




}
